﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public static class StringUtil
    {
        private static char[] Letters = new char[]
        {
            'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u',
            'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G',
            'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z'
        };
        private static char[] Numbers = new char[]
        {
            '1', '2', '3', '4', '5', 
            '6', '7', '8', '9', '0'
        };

        public static string RemoveLastWord(string str)
        {
            string[] kro = str.Split(' ');
            string finalString = "";
            for (int i = 0; i < kro.Length - 1; i++)
            {
                finalString += kro[i];
                if (i != kro.Length - 2)
                    finalString += ' ';
            }
            return finalString;
        }

        public static bool IsLetter(char ch)
        {
            return Letters.Contains(ch);
        }
        public static bool IsNumber(char nu)
        {
            return Numbers.Contains(nu);
        }
        public static bool IsSymbol(char sy)
        {
            return !IsLetter(sy) && !IsNumber(sy);
        }

        public static string[] BreakString(string str, string breakLine)
        {
            if (str.Contains(breakLine))
            {
                string[] sr = str.Split(new string[] {breakLine}, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < sr.Length; i++)
                {
                    if (sr[i].StartsWith(" "))
                    {
                        sr[i] = sr[i].Remove(0, 1);
                    }
                }

                return sr;
            }
            return new string[] { str };
        }

        /// <summary>
        /// Gives the number of times the same char appears inside
        /// the String
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int CountOfCharOnString(string value, char ch)
        {
            int count = 0;

            for (int i = 0; i < value.Length; i++)
            {
                if (value[i] == ch)
                    count++;
            }

            return count;
        }

        /// <summary>
        /// Converts the Specified string to an Object.
        /// How this works:
        /// VECTOR3: X:Y:Z
        /// FLOAT  : Must have a , or .
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object ConvertToObject(string value)
        {
            if (CountOfCharOnString(value, ':') == 2) // Vector3
            {
                return Vector3Util.CreateFromString(value);
            }
            int countOfDot = CountOfCharOnString(value, '.');
            int countOfVir = CountOfCharOnString(value, ',');
            if (countOfDot == 1 ||
                countOfVir == 1) // float
            {
                return FloatUtil.CreateFromString(value);
            }
            if (countOfDot == 0 &&
                countOfVir == 0) // int
            {
                return IntUtil.CreateFromString(value);
            }

            return null;
        }
    }
}
